//
//  ClassDetailViewController.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/23.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "ClassDetailViewController.h"
#import "ZXFDXuanZeXueShengFileHeaderView.h"
#import "ZXFDCreateClassView.h"
#import "SYWordViewController.h"
#import "FuDaoShiPingGuanKanJiLuView.h"
#import "SYFDXQTableTableViewCell.h"
#import "SYClassDetailsView.h"


@interface ClassDetailViewController ()

@property (strong, nonatomic) SYNoLabel *nolab;
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (strong, nonatomic) NSArray *oneArr;
@property (strong, nonatomic) NSArray *oneContArr;
@property (strong, nonatomic) ZXFDCreateClassView *createClassView;
@property (strong, nonatomic) SYWordViewController *playerVC;
@property (strong, nonatomic) FuDaoShiPingGuanKanJiLuView *fuDaoGuanKanJiLuView;
@property (copy, nonatomic) NSString *fudaoid;//辅导id
@property (copy, nonatomic) NSString *classId;
@property (strong, nonatomic) SYClassDetailsView *classDetailsView;


@end

@implementation ClassDetailViewController
- (void)dealloc {
    //移除所有观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
        [self layout];
    }
    return self;
}
//kNotification
- (void)initSubView{
    
    _oneArr = @[@"班级名称：",@"辅导老师：",@"助教：",@"辅导类型：",@"状态：",@"辅导内容："];
    
    [self addSubview:self.navView];
    [self addSubview:self.tableView];
    _nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableView:) name:kNotification object:nil];
    [self showAlterDetailsView];
}
- (void)refreshTableView: (NSNotification *) notification {
    //处理消息
    [self requestDataClassId:self.classId];
}
- (void)layout{
    [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@40);
        make.right.equalTo(@0).offset(-40);
        make.top.equalTo(self->_navView.mas_bottom);
        make.bottom.equalTo(@0);
    }];
}

- (void)requestDataClassId:(NSString *)classId {
    _classId = classId;
    [MBProgressHUD mj_showMessage:nil toView:self];
    if (classId.length == 0) {
        classId = @"0";
    }
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestZaiXianClassDetailsClassId:classId success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        if (listArr.count == 0) {
            weakSelf.nolab.hidden = NO;
            weakSelf.tableView.hidden = YES;
        } else {
            weakSelf.dataListArr = listArr;
            SYBanJiDetails *bjd = listArr.firstObject;
            weakSelf.fudaoid = bjd.bjid;
            weakSelf.oneContArr = @[bjd.title,bjd.accountName,bjd.tutorAccountName,bjd.typeName,bjd.statusName,bjd.content];
            [weakSelf.tableView reloadData];
        }
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
//         weakSelf.nolab.hidden = NO;
//         weakSelf.tableView.hidden = YES;
    }];
}


- (ClassDetailVCNavView *)navView{
    if (!_navView) {
        ClassDetailVCNavView *view = [[ClassDetailVCNavView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        view.backgroundColor = MAINBACKCOLOR;
        view.titleLabe.text = @"班级详情";
        view.delegate = self;
        [view initOption];
        _navView = view;
    }
    return _navView;
}
- (void)backClick{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
- (void)submiteClick{
    
}
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[ClassDetailTBCellOfTop class] forCellReuseIdentifier:@"ClassDetailTBCellOfTop"];
        [tableView registerClass:[CounselingIformationCell class] forCellReuseIdentifier:@"CounselingIformationCell"];
        [tableView registerClass:[CounselingIformationButtonCell class] forCellReuseIdentifier:@"CounselingIformationButtonCell"];
        [tableView registerClass:[CourseStudentOfClassDetailCell class] forCellReuseIdentifier:@"CourseStudentOfClassDetailCell"];
        [tableView registerClass:[CDHAndZiYuanCell class] forCellReuseIdentifier:@"CDHAndZiYuanCell"];
        [tableView registerCellWithNIbNames:@[@"SYFDXQTableTableViewCell"]];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView = tableView;
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //TOP
    SYBanJiDetails *bjd = self.dataListArr.firstObject;
    
    if (section == 0) {
        return 6;
    } else if (section == 1) {
        return bjd.dataTutorArr.count+1;
    } else if (section == 2) {
        return bjd.dataStudentArr.count+1;
    } else if (section == 3) {
        SYDataResourceModel *resourceModel = bjd.dataResourceArr.firstObject;
        
        return resourceModel.dataResourceFile.count;
//        return 5;
    }
    
    
//    if (section == 0) {
//        return 6;
//    }else{
//        return 5;
//    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 3) {
        return 44+40;
    }
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //TOP
    SYBanJiDetails *bjDetails = self.dataListArr.firstObject;
    if (indexPath.section == 0) {
        ClassDetailTBCellOfTop *cell = [tableView dequeueReusableCellWithIdentifier:@"ClassDetailTBCellOfTop" forIndexPath:indexPath];
        if (!cell) {
            cell = [[ClassDetailTBCellOfTop alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ClassDetailTBCellOfTop"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.text = _oneArr[indexPath.row];
        cell.contentLabel.text = _oneContArr[indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            CounselingIformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CounselingIformationCell" forIndexPath:indexPath];
            if (!cell) {
                cell = [[CounselingIformationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CounselingIformationCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.nameArray = @[@"课次",@"开始日期",@"时长/分",@"状态",@"考勤",@"观看",@"视频",@"详情"];
            [cell reloadViewLayout];
            return cell;
        }else{
            SYDataTutorModel *tutorModel = bjDetails.dataTutorArr[indexPath.row -1];
            
            SYFDXQTableTableViewCell *fxqqCell = [tableView dequeueReusableCellWithIdentifier:@"SYFDXQTableTableViewCell" forIndexPath:indexPath];
            fxqqCell.selectionStyle = UITableViewCellSelectionStyleNone;
            fxqqCell.block = ^(NSInteger index) {
                if (index == 1) {//观看
                    [self.mainDelegate lookImageClick:bjDetails.bjid];
                } else if (index == 2) {
                    SYDataTutorModel *tutor = bjDetails.dataTutorArr[indexPath.row -1];
                    [self.mainDelegate videoImageClick:tutor.videoPath];
                } else { //详情
                    
                    self.classDetailsView.hidden = NO;
                    self.classDetailsView.tutorModel = tutorModel;
                }
            };
            
            
            fxqqCell.tutorModel = tutorModel;
            return fxqqCell;
//            CounselingIformationButtonCell*cell = [tableView dequeueReusableCellWithIdentifier:@"CounselingIformationButtonCell" forIndexPath:indexPath];
//            if (!cell) {
//                cell = [[CounselingIformationButtonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CounselingIformationButtonCell"];
//            }
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
////            cell.nameArray = @[@"1",@"2019-01-30 18:00",@"2019-01-30 18:00",@"40",@"开始",@"3/4"];
////            cell.nameArray = @[@"课次",@"开始日期",@"时长/分",@"状态",@"考勤",@"观看",@"视频",@"详情"];
//            NSString *kaoqin = [NSString stringWithFormat:@"%@/%@",tutorModel.attendStudents,tutorModel.totalstudents];
//            cell.nameArray = @[tutorModel.lessonTimes,tutorModel.beginDate,tutorModel.duration,tutorModel.statusName,kaoqin,@""];
//            cell.indexPath = indexPath;
//            cell.lookBlock = ^(NSIndexPath * _Nonnull indexPath) {
//                [self.mainDelegate lookImageClick:bjDetails.bjid];
//            };
//            cell.videoBlock = ^(NSIndexPath * _Nonnull indexPath) {
//                SYDataTutorModel *tutor = bjDetails.dataTutorArr[indexPath.row -1];
//                [self.mainDelegate videoImageClick:tutor.videoPath];
//            };
//
//            //TOP
////            cell.nameArray = bjDetails.dataTutor;
//
//
//            [cell reloadViewLayout];
//            return cell;
        }
        
        
    }else if (indexPath.section  == 2){
        if (indexPath.row == 0) {
            CourseStudentOfClassDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseStudentOfClassDetailCell" forIndexPath:indexPath];
            if (!cell) {
                cell = [[CourseStudentOfClassDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CourseStudentOfClassDetailCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.nameArray = @[@"学号",@"姓名",@"性别",@"联系人",@"电话",@"状态",@"入学时间"];
            [cell reloadViewLayout];
            return cell;
        }else{
            CourseStudentOfClassDetailCell*cell = [tableView dequeueReusableCellWithIdentifier:@"CourseStudentOfClassDetailCell" forIndexPath:indexPath];
            if (!cell) {
                cell = [[CourseStudentOfClassDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CourseStudentOfClassDetailCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            cell.nameArray = @[@"S21576",@"郑宇航",@"男",@"王玉敏",@"13926586423",@"出清异常",@"2019-01-30"];
            //TOP
            SYDataStudent *dataStudent = bjDetails.dataStudentArr[indexPath.row - 1];
            NSString *sext = [dataStudent.sex integerValue] == 1?@"男":[dataStudent.sex integerValue] == 2?@"女":@"未知";
           // NSArray *timeArr = [dataStudent.inDate componentsSeparatedByString:@" "];
            NSInteger index = [self timeSwitchTimestamp:dataStudent.inDate andFormatter:@"YYYY-MM-dd HH:mm:ss"];
            NSString *strtime = [self timestampSwitchTime:index andFormatter:@"YY-MM-dd"];
            cell.nameArray = @[dataStudent.serial,dataStudent.name,sext,dataStudent.father,dataStudent.mobile,dataStudent.statusName,strtime];
            
            [cell reloadViewLayout];
            return cell;
        }
    }else{
        CDHAndZiYuanCell*cell = [tableView dequeueReusableCellWithIdentifier:@"CDHAndZiYuanCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[CDHAndZiYuanCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CDHAndZiYuanCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //TOP
        SYDataResourceModel *resource = bjDetails.dataResourceArr.firstObject;
        SYDataResourceFileModel *fileModel = resource.dataResourceFile[indexPath.row];
        cell.titleLabel.text = fileModel.name;
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:fileModel.icon] placeholderImage:[UIImage imageNamed:@""]];
        cell.detailLabel.text = fileModel.size;
        cell.detailLabel.text = fileModel.duration;
        
        
        return cell;
    }
    
}


- (void)showAlterDetailsView {
    self.classDetailsView = [[NSBundle mainBundle] loadNibNamed:@"SYClassDetailsView" owner:nil options:nil].firstObject;
    self.classDetailsView.hidden = YES;
    [self addSubview:self.classDetailsView];
    
    [self.classDetailsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self);
    }];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //NSLog(@"点击了cell");
    if (indexPath.section == 3) {
        _playerVC = [[SYWordViewController alloc] init];
        _playerVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:_playerVC animated:YES completion:nil];
        
        
    }
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 3) {
        SYBanJiDetails *bjDetails = self.dataListArr.firstObject;
//        TOP
        SYDataResourceModel *drmodel = bjDetails.dataResourceArr.firstObject;
        
        
        _nextHeader = [[CDHAndZiYuanHeader alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44+50)];
        _nextHeader.nameLabel.text = @"班级资源";
        //TOP
        _nextHeader.fixButton.hidden = YES;
        _nextHeader.titleLabel.text = drmodel.courseName;
        _nextHeader.detailLabel.text = [NSString stringWithFormat:@"文件：%@  大小：%@MB",drmodel.files,drmodel.size];
        
        return _nextHeader;
    }else{
        _header = [[ClassDetailDetailCellHeader alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        switch (section) {
            case 0:
            {
                _header.nameLabel.text = @"班级信息";
                
                __weak typeof(self) weakSelf = self;
                _header.block = ^(NSInteger index) {
                    weakSelf.createClassView = [[ZXFDCreateClassView alloc]initWithFrame:CGRectMake(0, 0, weakSelf.frame.size.width, weakSelf.frame.size.height)];
                    weakSelf.createClassView.isXiu = !weakSelf.isDetails;
                    weakSelf.createClassView.fdId = weakSelf.fudaoid;
                    weakSelf.createClassView.frame = CGRectMake(0, 0, weakSelf.frame.size.width, weakSelf.frame.size.height);
                    weakSelf.createClassView.backgroundColor = [UIColor whiteColor];
                    [weakSelf addSubview:weakSelf.createClassView];
                    [UIView animateWithDuration:0.5 animations:^{
                        weakSelf.createClassView.frame = CGRectMake(0, 0,weakSelf.frame.size.width,weakSelf.frame.size.height);
                    } completion:^(BOOL finished) {
                        //        [MBProgressHUD hideHUDForView:self.view animated:YES];
                    }];

                };
                
            }
                break;
            case 1:
            {
                _header.nameLabel.text = @"辅导信息";
                _header.fixButton.hidden = YES;
            }
                break;
            case 2:
            {
                _header.nameLabel.text = @"班级学生";
                _header.fixButton.hidden = YES;
            }
                break;
            case 3:
            {
                
            }
                break;
                
            default:
                break;
        }
        return _header;
    }
    
    
}





- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}

- (void)tiaozhuanPlayerDetails:(NSString *)classid {
    _fuDaoGuanKanJiLuView = [[FuDaoShiPingGuanKanJiLuView alloc]initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
    _fuDaoGuanKanJiLuView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_fuDaoGuanKanJiLuView];
    [UIView animateWithDuration:0.5 animations:^{
        [self.fuDaoGuanKanJiLuView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.equalTo(self);
        }];
        
    } completion:^(BOOL finished) {
        
    }];
}

-(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSLog(@"1296035591  = %@",confromTimesp);
    
    
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    
    
    //NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);
    
    
    
    return confromTimespStr;
    
}
- (NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    
    
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}



@end

















